package com.lczyfz.campusinfo.modules.thirdpay.service;

import com.lczyfz.campusinfo.modules.thirdpay.wxpay.WXPay;
import com.lczyfz.campusinfo.modules.thirdpay.wxpay.impl.WXPayConfigImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by maple on 2017-07-15.
 */
@Service
public class WxpayService {
    private WXPay wxpay;
    private WXPayConfigImpl config;

    public WxpayService() {
        try {
            config = WXPayConfigImpl.getInstance();
            wxpay = new WXPay(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 扫码支付  下单
     */
    public Map doUnifiedOrder(String body, String out_trade_no, int total_fee, String product_id) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("body", body);
        data.put("out_trade_no", out_trade_no);
        data.put("device_info", "WEB");
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee + "");
        data.put("spbill_create_ip", "58.22.125.84");
        data.put("notify_url", "http://www.ledog.cn/i/wxpay/wxPayCallBack");
        data.put("trade_type", "NATIVE");
        data.put("product_id", product_id);
        // data.put("time_expire", "20170112104120");
        Map<String, String> r = wxpay.unifiedOrder(data);
        System.out.println(r);

        return r;
    }

    public Map doOrderQuery(String out_trade_no) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
//        data.put("transaction_id", "4008852001201608221962061594");

        Map<String, String> r = wxpay.orderQuery(data);
        System.out.println(r);
        return r;
    }
    /**
     * 退款
     */
    public Map doRefund(String out_trade_no, int total_fee, int refund_fee) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
        data.put("out_refund_no", out_trade_no);
        data.put("total_fee", total_fee + "");
        data.put("refund_fee", refund_fee + "");
        data.put("refund_fee_type", "CNY");
        data.put("op_user_id", config.getMchID());


        Map<String, String> r = wxpay.refund(data);
        System.out.println(r);

        return r;
    }

    /**
     * H5端支付  下单
     */
    public Map doUnifiedOrderByH5(String body, String out_trade_no, int total_fee, String product_id) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("body", body);
        data.put("out_trade_no", out_trade_no);
        data.put("device_info", "H5");
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee + "");
        data.put("spbill_create_ip", "58.22.125.84");
        data.put("notify_url", "http://www.ledog.cn/i/wxpay/wxPayCallBack");
//        data.put("notify_url", "http://test.kjfax.cn/i/wxpay/wxPayCallBack");

        data.put("trade_type", "MWEB");
        data.put("product_id", product_id);
        // data.put("time_expire", "20170112104120");
        Map<String, String> r = wxpay.unifiedOrder(data);
        System.out.println(r);


        return r;
    }

    /**
     * 公众号端支付  下单
     */
    public Map doUnifiedOrderByPublicNumber(String body, String out_trade_no, int total_fee, String product_id, String openid,String ip) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("body", body);
        data.put("out_trade_no", out_trade_no);
        data.put("device_info", "WEB");
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee + "");
        //APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP
        data.put("spbill_create_ip", ip);
//        data.put("notify_url", "http://www.ledog.cn/i/wxpay/wxPayCallBack");
        //异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。
        data.put("notify_url", "http://campusinfo.lczyfz.com/i/wxpay/wxPayCallBack");
        data.put("trade_type", "JSAPI");
        data.put("product_id", product_id);
        data.put("openid", openid);
        // data.put("time_expire", "20170112104120");
        Map<String, String> r = wxpay.orderQuery(data);
        System.out.println(r);
        return r;
    }
    /**
     * H5端支付  下单
     */
    public Map doUnifiedOrderByAPP(String body, String out_trade_no, int total_fee, String product_id) throws Exception {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("body", body);
        data.put("out_trade_no", out_trade_no);
        data.put("device_info", "APP");
        data.put("fee_type", "CNY");
        data.put("total_fee", total_fee + "");
        data.put("spbill_create_ip", "58.22.125.84");
        data.put("notify_url", "http://www.ledog.cn/i/wxpay/wxPayCallBack");
        data.put("trade_type", "APP");
        data.put("product_id", product_id);
        // data.put("time_expire", "20170112104120");
        Map<String, String> r = wxpay.unifiedOrder(data);
        System.out.println(r);


        return r;
    }
}
